﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
using MongoDB.Linq;
using Blog_Domain;

namespace BlogApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a default mongo object
            // Connect to localhost, port 27017
            var mongo = new Mongo();
            mongo.Connect();

            //Get the blog database. If it doesn't exit, Mongo will create it 
            //for us when we first use it.
            var db = mongo.GetDatabase("blog");

            var postCollection = db.GetCollection<Post>();

            //Delete all Post
            postCollection.Remove(p => true);

            CreatePosts(postCollection);

            //Count all the posts
            var totalNumberOfPosts = postCollection.Count();

            //Count only the posts that have 1 comment
            var numberOfPostsWith1Comment = postCollection.Count(p => p.Comments.Count == 1);

            //find the titles of the posts that gaupo commented on...
            var postsThatGaupoCommentedOn = from p in postCollection.Linq()
                                            where p.Comments.Any(c => c.Email.StartsWith("gaupo"))
                                            select p.Title;

            //find the titles and comments of the posts that have comments after January 1st
            var postsWithJanuary1st = from p in postCollection.Linq()
                                      where p.Comments.Any(c => c.TimePosted > new DateTime(2013, 1, 1))
                                      select new { Title = p.Title, Comments = p.Comments };

            //find the posts with less than 40 characters
            var postsWithLessThan10Words = from p in postCollection.Linq()
                                           where p.CharCount < 40
                                           select p;

            Console.WriteLine(String.Format("Total number of posts : {0}", totalNumberOfPosts));
            Console.WriteLine(String.Format("Total number of posts have only one comment: {0}", numberOfPostsWith1Comment));
            Console.WriteLine("Posts are posted by gaupo:");
            foreach(var post in postsThatGaupoCommentedOn)
            {
                Console.WriteLine(String.Format("_ {0}", post.ToString()));
            }
            Console.ReadLine();

        }

        private static void CreatePosts(IMongoCollection<Post> collection)
        {
            var post = new Post()
            {
                Title = "My first Post",
                Body = "This is my first post, which is about MongoDB.",
                CharCount = 28,
                Comments = new List<Comment>
                {
                    { new Comment() { TimePosted = DateTime.Now, Email = "gaupoit@gmail.com", Body = "This article is too short!" } },
                    { new Comment() { TimePosted = DateTime.Now, Email = "thinhnp60581@gmail.com", Body = "I agree with #1" } }
                }
            };

            collection.Save(post);

            var post1 = new Post()
            {
                Title = "My Second Post",
                Body = "This is second post, which is about SQL Server 2012.",
                CharCount = 30,
                Comments = new List<Comment>
                {
                    { new Comment() { TimePosted = DateTime.Now, Email = "bob_mcbob@gmail.com", Body = "This isn't any better" } },
                }
            };

            collection.Save(post1);
        }
    }
}
